﻿using System;
using System.Diagnostics;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Resources;
using System.Windows;

namespace MainApp.Utils
{
    public class TrayIconTool
    {
        private WindowState _oldState = WindowState.Normal;
        private string _text = string.Empty;
        private string _balloonTipTitle = string.Empty;
        private string _balloonTipText = string.Empty;
        private System.Windows.Forms.NotifyIcon _notifyIcon;
        private MainWindow _mainWindow;

        /// <summary>
        /// 用户点击通知图标处理
        /// </summary>
        public event EventHandler OnClick;
        /// <summary>
        /// 获取或设置当前鼠标停留在通知图标上时显示的工具提示文本
        /// </summary>
        public string Text
        {
            get { return _text; }
            set
            {
                if (_text != value)
                {
                    _text = value;
                    ChangeText(value);
                }
            }
        }
        /// <summary>
        /// 获取或设置要作为气球装提示的标题显示的文本
        /// </summary>
        public string BalloonTipTitle
        {
            get { return _balloonTipTitle; }
            set
            {
                if (_balloonTipTitle != value)
                {
                    _balloonTipTitle = value;
                    ChangeBalloonTipTitle(value);
                }
            }
        }
        /// <summary>
        /// 获取或设置要作为气球装提示显示的文本
        /// </summary>
        public string BalloonTipText
        {
            get { return _balloonTipText; }
            set
            {
                if (_balloonTipText != value)
                {
                    _balloonTipText = value;
                    ChangeBalloonTipText(value);
                }
            }
        }
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="window"></param>
        public TrayIconTool(MainWindow window)
        {
            _text = $"PNT启动程序";
            _mainWindow = window;
            _balloonTipTitle = _text;
            _balloonTipText = $"{_text}已最小化到这里！";
            _oldState = window.WindowState;
            CreateTrayIcon();
        }
        /// <summary>
        /// 创建托盘图标
        /// </summary>
        private void CreateTrayIcon()
        {
            var trayIcon = new System.Windows.Forms.NotifyIcon();
            trayIcon.Icon = LoadAppIcon();
            trayIcon.Text = Text;
            trayIcon.BalloonTipTitle = BalloonTipTitle;
            trayIcon.BalloonTipText = BalloonTipText;
            trayIcon.BalloonTipIcon = System.Windows.Forms.ToolTipIcon.Info;
            trayIcon.ContextMenu = CreateContextMenu();
            trayIcon.Click += TrayIcon_Click;
            trayIcon.Visible = true;
            _notifyIcon = trayIcon;
        }
        private Icon LoadAppIcon()
        {
            //设置窗口图标
            using (var stream = GetResourceStream(GetType().Assembly, "app.ico"))
            {
                if (stream == null)
                    return null;
                try { return new Icon(stream); } catch { }
            }
            return null;
        }
        private Stream GetResourceStream(Assembly assembly, string name)
        {
            string resourceName = assembly.GetName().Name + ".g";
            var resourceManager = new ResourceManager(resourceName, assembly);
            if (resourceManager != null)
            {
                //获取指定名称的资源
                return resourceManager.GetStream(name.ToLower(), CultureInfo.CurrentCulture);
            }
            return null;
        }
        
        private System.Windows.Forms.ContextMenu CreateContextMenu()
        {
            var contextMenu = new System.Windows.Forms.ContextMenu();
            contextMenu.Popup += (s, e) =>
            {
            };
            contextMenu.MenuItems.Add("主界面", (s, e) =>
            {
                if (_mainWindow == null)
                    return;
                _mainWindow.ActiveOrDeactiveWindow(false);
            });
            contextMenu.MenuItems.Add("退出", (s, e) =>
            {
                Process.GetCurrentProcess().Kill();
            });
            return contextMenu;
        }
        /// <summary>
        /// 点击图标处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TrayIcon_Click(object sender, EventArgs e)
        {
            if (e is System.Windows.Forms.MouseEventArgs mouseEventArgs &&
                mouseEventArgs.Button != System.Windows.Forms.MouseButtons.Left)
                return;
            OnClick?.Invoke(sender, e);
            var window = _mainWindow;
            if (window == null)
                return;
            window.ActiveOrDeactiveWindow();
        }
        /// <summary>
        /// 切换图标显示标题
        /// </summary>
        /// <param name="text"></param>
        private void ChangeText(string text)
        {
            var trayIcon = _notifyIcon;
            if (trayIcon == null)
                return;
            trayIcon.Text = text;
        }
        /// <summary>
        /// 切换气球图标显示标题
        /// </summary>
        /// <param name="text"></param>
        private void ChangeBalloonTipTitle(string text)
        {
            var trayIcon = _notifyIcon;
            if (trayIcon == null)
                return;
            trayIcon.BalloonTipTitle = text;
        }
        /// <summary>
        /// 切换气球图标显示文本
        /// </summary>
        /// <param name="text"></param>
        private void ChangeBalloonTipText(string text)
        {
            var trayIcon = _notifyIcon;
            if (trayIcon == null)
                return;
            trayIcon.BalloonTipText = text;
        }
        /// <summary>
        /// 显示托盘通知
        /// </summary>
        /// <param name="showTimes"></param>
        public void ShowBalloon(int showTimes = 2000)
        {
            var trayIcon = _notifyIcon;
            if (trayIcon == null)
                return;
            trayIcon.ShowBalloonTip(showTimes);
        }
        /// <summary>
        /// 关闭托盘图标
        /// </summary>
        public void Close()
        {
            var trayIcon = _notifyIcon;
            if (trayIcon == null)
                return;
            trayIcon.Visible = false;
            trayIcon.Dispose();
            _notifyIcon = null;
        }
    }
}
